package com.example.generator.controller;

import com.example.generator.entity.TableInfo;
import com.example.generator.entity.config.BaseInfoConfig;
import com.example.generator.entity.vo.CodeParament;
import com.example.generator.service.ICodeGeneratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author：QPX
 * @date：2024-01-26-11:21
 */
@Controller
@RequestMapping("/codeGenerator")
public class CodeGeneratorController {
    @Autowired
    private ICodeGeneratorService generatorService;


    @GetMapping("/index.html")
    public String view() {
        return "index/generator";
    }

    @ResponseBody
    @PostMapping("/code")
    public void generatorBase(@RequestBody CodeParament parament) throws Exception {
        TableInfo tableInfo = generatorService.generatorInit(parament);
        //生成mybatisPlus分页插件配置
        BaseInfoConfig baseInfoConfig = parament.getBaseInfoConfig();
        if (baseInfoConfig.getIsGeneratorSelect()) {
            if (baseInfoConfig.getIsGeneratorMybatisPlusConfig()) {
                generatorService.generatorMybatisPlusConfig(tableInfo, parament);
            }
            if ("".equals(baseInfoConfig.getIsGeneratorR()) || baseInfoConfig.getIsGeneratorR() == null) {
                generatorService.generatorR(tableInfo, parament);
            } else {
                tableInfo.setReClassPackage(baseInfoConfig.getIsGeneratorR());
            }
        }
        //基本代码生成
        generatorService.generatorEntity(tableInfo, parament);
        generatorService.generatorMapper(tableInfo, parament);
        generatorService.generatorMapperXml(tableInfo, parament);
        generatorService.generatorService(tableInfo, parament);
        //Excel生成
        if (baseInfoConfig.getIsGeneratorExcelImport()) {
            generatorService.generatorExcelImport(tableInfo, parament);
        }
        generatorService.generatorController(tableInfo, parament);
    }


}
